package com.nxy.dao;

import com.nxy.utils.JdbcUtils;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class BaseDao<T> {

    public int update(String sql,Object...params){
        try {
            int n = JdbcUtils.qr.update(sql,params);
            return n;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    public T queryOne(String sql,Class<T> clazz, Object...params){
        try {
            return JdbcUtils.qr.query(sql,new BeanHandler<T>(clazz),params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    public List<T> queryMore(String sql, Class<T> clazz, Object...params){
        try {
            return JdbcUtils.qr.query(sql,new BeanListHandler<T>(clazz),params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    public int queryScalar(String sql,Object...params){
        try {
            return JdbcUtils.qr.query(sql,new ScalarHandler<Number>(),params).intValue();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}
